package com.shop.model.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.model.entity.AttributeTb;
import com.shop.model.entity.DescribeImg;
import com.shop.model.entity.GoodsTb;
import com.shop.model.service.IAttributeTbService;
import com.shop.model.service.IDescribeImgService;
import com.shop.model.service.IGoodsTbService;
import com.shop.model.util.AttributeUtil;
import com.shop.model.util.GoodsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 一个控制商品详情的控制器
 */
@Controller
public class DetailController {

    @Autowired
    private IGoodsTbService iGoodsTbService;
    @Autowired
    private IAttributeTbService iAttributeTbService;
    @Autowired
    private IDescribeImgService iDescribeImgService;
    @RequestMapping("/details")
    public String detail(HttpServletRequest request){

        //获取session
        HttpSession session = request.getSession();
        Integer userId = null;
        //如果获取用户id失败，抛出异常，跳转到登陆页
        try{
            userId = (int)session.getAttribute("userId");
        }catch (Exception e){
            return "redirect:/login";
        }
        //查询到当前商品的基本信息
        GoodsTb goodsResult =  iGoodsTbService.getOne(new QueryWrapper<GoodsTb>().eq("goods_id",request.getParameter("id")));
        //把查到的信息给放到request里面
        request.setAttribute("goods",goodsResult);
        //获取到商品信息的属性的id集合
        String attributesId = goodsResult.getAttributesId();
        //将id字符串集合转换为int数组
        Integer[] integers = GoodsUtil.stringSplitToIntegerArray(attributesId, ",");
        ArrayList<AttributeTb> attributeTbs = new ArrayList<>();
        //进行属性id的分割，然后运用属性id去进行查询得到对应的属性集合
        for (int i = 0; i < integers.length; i++) {
            AttributeTb attribute =new AttributeTb();
            attribute.setId(integers[i]);
            AttributeTb attributeResult = iAttributeTbService.getById(integers[i]);
            attributeTbs.add(attributeResult);
        }
        //把属性进行组装成为（属性名和属性值数组）
        HashMap<String, String[]> stringHashMap = AttributeUtil.attributeReorganization(attributeTbs);

        request.setAttribute("attributes",stringHashMap);
//        DescribeImg describeImg = new DescribeImg();
//        describeImg.setGoodsId(goodsResult.getDescribeImgId());
        Map<String, Object> describeImgs = iDescribeImgService.getMap(new QueryWrapper<DescribeImg>().eq("goods_id",goodsResult.getDescribeImgId()));
        String[] imgs =new String[describeImgs.size()];
        int i = 0;
        for (String s : describeImgs.keySet()) {
            imgs[i]= String.valueOf(describeImgs.values().toArray()[0]);
        }
        request.setAttribute("imgs",imgs);



        //通过商品的类型来获取当前商品类型的热销商品
        List<GoodsTb> hotGoodsBySpeciesId = iGoodsTbService.list(new QueryWrapper<GoodsTb>().eq("specie_id", goodsResult.getSpecieId()));
        List<GoodsTb> goodsTbList = GoodsUtil.randomNum(hotGoodsBySpeciesId, 6);
        request.setAttribute("hotGoods",goodsTbList);
        return "/details";
    }
}
